﻿/**************************************************************************
* @ file    : win32.h
* @ author  : yang yang && qinhongjie
* @ version : 1.0
* @ date    : 2020.03.30
* @ brief   : windows下编译头文件, 仅用于windows下模拟调试使用
*
* @Copyright (c) 2016  chuangmi inc.
***************************************************************************/
 
#ifndef __WIN32_H__
#define __WIN32_H__

#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#define __WINDOWS__
#endif

#ifdef __WINDOWS__

#include <io.h>     // need for: open, close
#include <direct.h> // need for: getcwd, chdir
#include <process.h>// need for: getpid
#include <windows.h>// need for: InterlockedExchangeAdd

#define strcasecmp stricmp

// 只是确保在windows下相关引用正常编译

#define SIGINT  2   
#define SIGQUIT 3  
#define SIGKILL 9
#define SIGUSR1 10
#define SIGUSR2 12

#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001

//#define S_IFMT 00170000
#define S_IFSOCK 0140000
#define S_IFLNK 0120000
//#define S_IFREG 0100000
#define S_IFBLK 0060000
//#define S_IFDIR 0040000
//#define S_IFCHR 0020000
#define S_IFIFO 0010000
#define S_ISUID 0004000
#define S_ISGID 0002000
#define S_ISVTX 0001000

#define msleep(ms)  Sleep(ms)
#define usleep(us)  Sleep((us) / 1000)

#else  // suppose GNU

#include <dlfcn.h>
#include <unistd.h>
#include <sys/time.h> 

// sleep for millisecond
#define msleep(ms)  usleep((ms) * 1000)

#define O_BINARY 0  // 去除linux下open不支持O_BINARY的问题

#endif /* __WINDOWS__ */

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __WINDOWS__

    /* ---------------- windows version of functions ---------------- */

    int gettimeofday(struct timeval *tp, void *tzp);

    char* strcasestr(const char *pString, const char *pFind);

    int fchmod(int fd, int mode);
    int mknod(const char *path, unsigned int mode, unsigned int dev);
    int link(const char *link_name, const char *target);
    int mkfifo(const char *name, unsigned int mode);

#else  //!__WINDOWS__

#endif /* __WINDOWS__ */

#ifdef __cplusplus
}
#endif

#endif // __WIN32_H__